package com.bjsxt.controller;

import com.bjsxt.mapper.SurveyDao;
import com.bjsxt.pojo.Question;
import com.bjsxt.pojo.Survey;
import com.bjsxt.service.QuestionServer;
import com.bjsxt.service.SurveyServer;
import com.bjsxt.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionServer questionServer;

    @RequestMapping(value = "/create")
    @ResponseBody
    public String create(Question question){
        int result = questionServer.create(question);
        if (result<=0){
            return "error";
        }
        return "success";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String ids){
        int i = questionServer.delete(ids);
        if (i<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Question question){
         int update = questionServer.update(question);
        if (update<=0){
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(@RequestBody Question question){

        //不传参即查询全部用户数据
        List<Question> data = questionServer.query(question);
        //不传参即查询用户总数
        int count = questionServer.count(question);

        return MapControl.getInstance().page(data,count).getMap();
    }


   @GetMapping("/detail")
    public String detail(Question question, ModelMap modelMap) {

            Question ad = questionServer.detail(question);
            modelMap.addAttribute("question",ad);
            return "question/update";
    }


    @GetMapping("/edit")
    public String edit(Question question, ModelMap modelMap) {

        Question ad = questionServer.detail(question);
        modelMap.addAttribute("question",ad);
        return "question/question";
    }

    @GetMapping("/list")
    public String list(){
        return "question/list";
    }

    @GetMapping("/add")
    public String v_add(){
        return "question/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String,Object> add(@RequestBody Question question, HttpServletRequest req){

        //成功则返回的值为新建问题的id，不成功则返回0
        int result = questionServer.create(question);

        if (result<=0){
            return MapControl.getInstance().error().getMap();
        }

        return MapControl.getInstance().success().add("id",result).getMap();
    }




}